{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.1 Numpy基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2.1.1 Numpy与数组**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4]\n",
      "[1 2 3 4]\n",
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = [1, 2, 3, 4]\n",
    "b = np.array([1, 2, 3, 4])\n",
    "print(a)\n",
    "print(b)\n",
    "print(type(a))\n",
    "print(type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "2\n",
      "[1, 2]\n",
      "[1 2]\n"
     ]
    }
   ],
   "source": [
    "# 接下来通过列表索引和数组索引来访问列表和数组中的元素，代码如下：\n",
    "print(a[1]) \n",
    "print(b[1])\n",
    "print(a[0:2]) \n",
    "print(b[0:2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上面结果可以看到列表和数组有着相同的索引机制，唯一的区别好像就是数组里面是通过空格分隔元素，而列表用的是逗号。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2.1.2 Numpy数组与列表的区别**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上面的分析得知Numpy数组和列表很类似，那么为什么Python又要创建一个Numpy库呢？其原因很多，这里主要讲两点：\n",
    "\n",
    "1.数组可以比较方便的进行一些数学运算，而列表则比较麻烦；\n",
    "\n",
    "2.数组可以支持多维的数据，而列表通常只能储存一维的数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 1, 2, 3, 4]\n",
      "[2 4 6 8]\n"
     ]
    }
   ],
   "source": [
    "c = a * 2\n",
    "d = b * 2\n",
    "print(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2], [3, 4], [5, 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "e = [[1,2], [3,4], [5,6]] # 列表里的元素为小列表\n",
    "f = np.array([[1,2], [3,4], [5,6]]) # 创建二维数组的一种方式\n",
    "print(e)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以看到列表虽然包含着三个小列表，但其还是一个一维的结构，而创建的二维数组则是一个三行两列的二维结构内容，这个也是之后学习pandas库的核心内容了，因为数据数据处理中经常用到二维数组，也即二维表格结构。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2.1.3 创建数组的几种方式**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 创建一维数组\n",
    "b = np.array([1, 2, 3, 4])\n",
    "# 创建二维数组\n",
    "f = np.array([[1,2], [3,4], [5,6]])\n",
    "\n",
    "print(b)\n",
    "print(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除此之外，还有一些常见的创建数组的方式，这里以一维数组为例，我们还可以采用np.arange()函数来产生一维数组，其中括号里可以选择1个或2个或3个参数，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n",
      "[5 6 7 8 9]\n",
      "[5.  5.5 6.  6.5 7.  7.5 8.  8.5 9.  9.5]\n"
     ]
    }
   ],
   "source": [
    "# 一个参数 参数值为终点，起点取默认值0，步长取默认值1\n",
    "x = np.arange(5)\n",
    "# 两个参数 第一个参数为起点，第二个参数为终点，步长取默认值1，左闭右开\n",
    "y = np.arange(5,10)\n",
    "# 三个参数 第一个参数为起点，第二个参数为终点，第三个参数为步长，左闭右开\n",
    "z = np.arange(5, 10, 0.5)\n",
    "print(x)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们还可以通过np.random模块来创建随机一维数组，比如可以通过np.random.randn(3)来创建一个服从正太分布（均值为0，方差为1的分布）的3个随机数一维数组，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.50437361  0.4235907  -0.08181017]\n"
     ]
    }
   ],
   "source": [
    "a = np.random.randn(3)\n",
    "print(a)  # 因为随机，所以每次运行的结果都会不太一样"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果把np.random.randn(3)换成np.random.rand(3)，那生成的就是0-1之间的3个随机数，这个在之后2.3.1小节演示绘制散点图的时候会用到。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "至于二维数组的创建与学习，可以利用一维数组中的np.arange()函数和reshape方法产生一个二维数组，比如将0到11个数转换成3行4列的二维数组，代码如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(12).reshape(3,4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里再简单提一种随机二维数组的创建，代码如下。其中np.random.randint()函数用来创建随机整数，括号里第一个元素0表示起始数，第二个元素10表示终止数，第三个元素(4, 4)则表示生成一个4行4列的二维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 9 9 2]\n",
      " [3 0 4 6]\n",
      " [0 2 1 9]\n",
      " [8 0 8 5]]\n"
     ]
    }
   ],
   "source": [
    "a = np.random.randint(0, 10, (4, 4))\n",
    "print(a)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
